﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.Entity;

namespace CMS.DAL
{
    public class ImageManageListDAL
    {
        public ImageManageList GetOne(long id)
        {
            string sql = "select * from ImageManageList where id=@id";
            Tools tool = new Tools();
            tool.AddSqlParameter("id", System.Data.SqlDbType.BigInt, id);
            return SqlHelper.GetDataByColumns<ImageManageList>(DBConn.CMS, sql, tool.GetSqlParameters());
        }

        public ImageManageList GetOne(long id, string type)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("select * from ImageManageList where [FileName] in");
            sb.AppendLine("(select [FileName] from ImageManageList where id=@id)");
            sb.AppendLine("and [type]=@type");
            Tools tool = new Tools();
            tool.AddSqlParameter("id", System.Data.SqlDbType.BigInt, id);
            tool.AddSqlParameter("type", System.Data.SqlDbType.VarChar, type);
            return SqlHelper.GetDataByColumns<ImageManageList>(DBConn.CMS, sb.ToString(), tool.GetSqlParameters());
        }

        public List<ImageManageList> GetLst(string fid)
        {
            string sql = "select * from ImageManageList where fid=@fid and type='small'";
            Tools tool = new Tools();
            tool.AddSqlParameter("fid", System.Data.SqlDbType.VarChar, fid);
            return SqlHelper.GetDataByColumnsLst<ImageManageList>(DBConn.CMS, sql, tool.GetSqlParameters());
        }

        public void Add(ImageManageList c)
        {
            string sql = "INSERT INTO [ImageManageList] ([Fid] ,[Title],[Type] ,[FileName] ,[ImageUrl] ,[Url],[RecTime] ,[ModTime]) VALUES (@Fid ,@Title,@Type ,@FileName ,@ImageUrl ,@Url,@RecTime ,@ModTime)";
            var sps = SqlHelper.ObjectToSqlParameters(c);
            SqlHelper.ExecuteNonQuery(DBConn.CMS, System.Data.CommandType.Text, sql, sps);
        }

        public void Update(ImageManageList c)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("UPDATE [ImageManageList]");
            sb.AppendLine("   SET [Fid] = @Fid");
            sb.AppendLine("      ,[Title]=@Title");
            sb.AppendLine("      ,[Type] = @Type");
            sb.AppendLine("      ,[FileName] = @FileName");
            sb.AppendLine("      ,[ImageUrl] = @ImageUrl");
            sb.AppendLine("      ,[Url]=@Url");
            sb.AppendLine("      ,[RecTime] = @RecTime");
            sb.AppendLine("      ,[ModTime] = @ModTime");
            sb.AppendLine(" WHERE id=@id");
            var sps = SqlHelper.ObjectToSqlParameters(c);
            SqlHelper.ExecuteNonQuery(DBConn.CMS, System.Data.CommandType.Text, sb.ToString(), sps);
        }

        public void Delete(long id)
        {
            var c = GetOne(id);
            string sql = "delete from ImageManageList where FileName=@FileName";
            Tools tool = new Tools();
            tool.AddSqlParameter("FileName", System.Data.SqlDbType.VarChar, c.FileName);
            SqlHelper.ExecuteNonQuery(DBConn.CMS, System.Data.CommandType.Text, sql, tool.GetSqlParameters());
        }

        public void Delete(string fid)
        {
            string sql = "delete from ImageManageList where fid=@fid";
            Tools tool = new Tools();
            tool.AddSqlParameter("fid", System.Data.SqlDbType.VarChar, fid);
            SqlHelper.ExecuteNonQuery(DBConn.CMS, System.Data.CommandType.Text, sql, tool.GetSqlParameters());
        }


    }
}
